package com.propertree.mailer.ui;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.propertree.mailer.config.MailerConfigContainer;
import com.propertree.mailer.publisher.MailPublisher;

public class MailerWindow {

	private JFrame frmPropertreeMailerApplication;
	private JTextField pausePerBlockField;
	private JTextField delayedStartField;
	private JTextField pausePerEmailField;
	private JTextField blockSizeField;
	private JTextField receiverFilePathLabel;
	private JTextField emailSubFilePathLabel;
	private JTextField emaiBodyFilePathLabel;
	private JTextField outputFilePathLabel;
	private JTextField senderFilePathLabel;
	private MailerConfigContainer mailerConfigContainer;
	private JTextArea outputField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MailerWindow window = new MailerWindow();
					window.frmPropertreeMailerApplication.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MailerWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		mailerConfigContainer = MailerConfigContainer.getInstance();

		frmPropertreeMailerApplication = new JFrame();
		frmPropertreeMailerApplication.setTitle("ProperTree Mailer Application");
		frmPropertreeMailerApplication.setBounds(100, 100, 900, 600);
		frmPropertreeMailerApplication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 40, 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 1.0, 1.0, Double.MIN_VALUE };
		frmPropertreeMailerApplication.getContentPane().setLayout(gridBagLayout);

		senderFilePathLabel = new JTextField();
		receiverFilePathLabel = new JTextField();
		emailSubFilePathLabel = new JTextField();
		emaiBodyFilePathLabel = new JTextField();
		outputFilePathLabel = new JTextField();

		JLabel lblSenderFilePath = new JLabel("Senders File Location");
		GridBagConstraints gbc_lblSenderFilePath = new GridBagConstraints();
		gbc_lblSenderFilePath.anchor = GridBagConstraints.WEST;
		gbc_lblSenderFilePath.insets = new Insets(0, 0, 5, 5);
		gbc_lblSenderFilePath.gridx = 1;
		gbc_lblSenderFilePath.gridy = 1;
		frmPropertreeMailerApplication.getContentPane().add(lblSenderFilePath, gbc_lblSenderFilePath);

		JButton sendersFilePathChooserButton = new JButton("Click and select senders' file");
		sendersFilePathChooserButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mailerConfigContainer.getSenderReceiverConfig().setSenderFilePath(setFilePath(senderFilePathLabel));
			}
		});
		sendersFilePathChooserButton.setToolTipText("Click and select senders' file");
		GridBagConstraints gbc_sendersFilePathChooserButton = new GridBagConstraints();
		gbc_sendersFilePathChooserButton.anchor = GridBagConstraints.WEST;
		gbc_sendersFilePathChooserButton.insets = new Insets(0, 0, 5, 0);
		gbc_sendersFilePathChooserButton.gridx = 2;
		gbc_sendersFilePathChooserButton.gridy = 1;
		frmPropertreeMailerApplication.getContentPane().add(sendersFilePathChooserButton,
				gbc_sendersFilePathChooserButton);

		senderFilePathLabel.setEditable(false);
		senderFilePathLabel.setColumns(10);
		GridBagConstraints gbc_senderFilePathLabel = new GridBagConstraints();
		gbc_senderFilePathLabel.insets = new Insets(0, 0, 5, 0);
		gbc_senderFilePathLabel.fill = GridBagConstraints.HORIZONTAL;
		gbc_senderFilePathLabel.gridx = 2;
		gbc_senderFilePathLabel.gridy = 2;
		frmPropertreeMailerApplication.getContentPane().add(senderFilePathLabel, gbc_senderFilePathLabel);

		JLabel lblReceiversFilePath = new JLabel("Receivers' File Location");
		GridBagConstraints gbc_lblReceiversFilePath = new GridBagConstraints();
		gbc_lblReceiversFilePath.anchor = GridBagConstraints.WEST;
		gbc_lblReceiversFilePath.insets = new Insets(0, 0, 5, 5);
		gbc_lblReceiversFilePath.gridx = 1;
		gbc_lblReceiversFilePath.gridy = 3;
		frmPropertreeMailerApplication.getContentPane().add(lblReceiversFilePath, gbc_lblReceiversFilePath);

		JButton receiverFilePathChooserButton = new JButton("Click and select receiver' file");
		receiverFilePathChooserButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mailerConfigContainer.getSenderReceiverConfig().setReceiverFilePath(setFilePath(receiverFilePathLabel));
			}
		});
		GridBagConstraints gbc_receiverFilePathChooserButton = new GridBagConstraints();
		gbc_receiverFilePathChooserButton.anchor = GridBagConstraints.WEST;
		gbc_receiverFilePathChooserButton.insets = new Insets(0, 0, 5, 0);
		gbc_receiverFilePathChooserButton.gridx = 2;
		gbc_receiverFilePathChooserButton.gridy = 3;
		frmPropertreeMailerApplication.getContentPane().add(receiverFilePathChooserButton,
				gbc_receiverFilePathChooserButton);

		receiverFilePathLabel.setEditable(false);
		receiverFilePathLabel.setColumns(10);
		GridBagConstraints gbc_receiverFilePathLabel = new GridBagConstraints();
		gbc_receiverFilePathLabel.insets = new Insets(0, 0, 5, 0);
		gbc_receiverFilePathLabel.fill = GridBagConstraints.HORIZONTAL;
		gbc_receiverFilePathLabel.gridx = 2;
		gbc_receiverFilePathLabel.gridy = 4;
		frmPropertreeMailerApplication.getContentPane().add(receiverFilePathLabel, gbc_receiverFilePathLabel);

		JLabel lblEmailSubjectFile = new JLabel("Email Subject File Location");
		GridBagConstraints gbc_lblEmailSubjectFile = new GridBagConstraints();
		gbc_lblEmailSubjectFile.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblEmailSubjectFile.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmailSubjectFile.gridx = 1;
		gbc_lblEmailSubjectFile.gridy = 5;
		frmPropertreeMailerApplication.getContentPane().add(lblEmailSubjectFile, gbc_lblEmailSubjectFile);

		JButton emailSubFilePathChooserButton = new JButton("Click and select email subject file");
		emailSubFilePathChooserButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mailerConfigContainer.getEmailAttribConfig().setEmailSubjectFile(setFilePath(emailSubFilePathLabel));
			}
		});
		GridBagConstraints gbc_emailSubFilePathChooserButton = new GridBagConstraints();
		gbc_emailSubFilePathChooserButton.anchor = GridBagConstraints.WEST;
		gbc_emailSubFilePathChooserButton.insets = new Insets(0, 0, 5, 0);
		gbc_emailSubFilePathChooserButton.gridx = 2;
		gbc_emailSubFilePathChooserButton.gridy = 5;
		frmPropertreeMailerApplication.getContentPane().add(emailSubFilePathChooserButton,
				gbc_emailSubFilePathChooserButton);

		emailSubFilePathLabel.setEditable(false);
		emailSubFilePathLabel.setColumns(10);
		GridBagConstraints gbc_emailSubFilePathLabel = new GridBagConstraints();
		gbc_emailSubFilePathLabel.insets = new Insets(0, 0, 5, 0);
		gbc_emailSubFilePathLabel.fill = GridBagConstraints.HORIZONTAL;
		gbc_emailSubFilePathLabel.gridx = 2;
		gbc_emailSubFilePathLabel.gridy = 6;
		frmPropertreeMailerApplication.getContentPane().add(emailSubFilePathLabel, gbc_emailSubFilePathLabel);

		JLabel lblEmailBodyFile = new JLabel("Email Body File Location");
		GridBagConstraints gbc_lblEmailBodyFile = new GridBagConstraints();
		gbc_lblEmailBodyFile.anchor = GridBagConstraints.WEST;
		gbc_lblEmailBodyFile.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmailBodyFile.gridx = 1;
		gbc_lblEmailBodyFile.gridy = 7;
		frmPropertreeMailerApplication.getContentPane().add(lblEmailBodyFile, gbc_lblEmailBodyFile);

		JButton emailBodyFilePathChooserButton = new JButton("Click and select email body file");
		emailBodyFilePathChooserButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mailerConfigContainer.getEmailAttribConfig().setEmailBodyFile(setFilePath(emaiBodyFilePathLabel));
			}
		});
		GridBagConstraints gbc_emailBodyFilePathChooserButton = new GridBagConstraints();
		gbc_emailBodyFilePathChooserButton.anchor = GridBagConstraints.WEST;
		gbc_emailBodyFilePathChooserButton.insets = new Insets(0, 0, 5, 0);
		gbc_emailBodyFilePathChooserButton.gridx = 2;
		gbc_emailBodyFilePathChooserButton.gridy = 7;
		frmPropertreeMailerApplication.getContentPane().add(emailBodyFilePathChooserButton,
				gbc_emailBodyFilePathChooserButton);

		emaiBodyFilePathLabel.setEditable(false);
		emaiBodyFilePathLabel.setColumns(10);
		GridBagConstraints gbc_emaiBodyFilePathLabel = new GridBagConstraints();
		gbc_emaiBodyFilePathLabel.insets = new Insets(0, 0, 5, 0);
		gbc_emaiBodyFilePathLabel.fill = GridBagConstraints.HORIZONTAL;
		gbc_emaiBodyFilePathLabel.gridx = 2;
		gbc_emaiBodyFilePathLabel.gridy = 8;
		frmPropertreeMailerApplication.getContentPane().add(emaiBodyFilePathLabel, gbc_emaiBodyFilePathLabel);

		JLabel lblOutputFileLocation = new JLabel("Output File Location");
		GridBagConstraints gbc_lblOutputFileLocation = new GridBagConstraints();
		gbc_lblOutputFileLocation.anchor = GridBagConstraints.WEST;
		gbc_lblOutputFileLocation.insets = new Insets(0, 0, 5, 5);
		gbc_lblOutputFileLocation.gridx = 1;
		gbc_lblOutputFileLocation.gridy = 9;
		frmPropertreeMailerApplication.getContentPane().add(lblOutputFileLocation, gbc_lblOutputFileLocation);

		JButton outputFilePathChooserButton = new JButton("Click and select outout file");
		outputFilePathChooserButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mailerConfigContainer.getMailPubConfig().setOutputFilePath(setFilePath(outputFilePathLabel));
			}
		});
		GridBagConstraints gbc_outputFilePathChooserButton = new GridBagConstraints();
		gbc_outputFilePathChooserButton.anchor = GridBagConstraints.WEST;
		gbc_outputFilePathChooserButton.insets = new Insets(0, 0, 5, 0);
		gbc_outputFilePathChooserButton.gridx = 2;
		gbc_outputFilePathChooserButton.gridy = 9;
		frmPropertreeMailerApplication.getContentPane().add(outputFilePathChooserButton,
				gbc_outputFilePathChooserButton);

		outputFilePathLabel.setEditable(false);
		outputFilePathLabel.setColumns(10);
		GridBagConstraints gbc_outputFilePathLabel = new GridBagConstraints();
		gbc_outputFilePathLabel.insets = new Insets(0, 0, 5, 0);
		gbc_outputFilePathLabel.fill = GridBagConstraints.HORIZONTAL;
		gbc_outputFilePathLabel.gridx = 2;
		gbc_outputFilePathLabel.gridy = 10;
		frmPropertreeMailerApplication.getContentPane().add(outputFilePathLabel, gbc_outputFilePathLabel);

		JLabel lblEmailBlockSize = new JLabel("Email Block Size");
		GridBagConstraints gbc_lblEmailBlockSize = new GridBagConstraints();
		gbc_lblEmailBlockSize.anchor = GridBagConstraints.WEST;
		gbc_lblEmailBlockSize.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmailBlockSize.gridx = 1;
		gbc_lblEmailBlockSize.gridy = 11;
		frmPropertreeMailerApplication.getContentPane().add(lblEmailBlockSize, gbc_lblEmailBlockSize);

		blockSizeField = new JTextField();
		blockSizeField.setText("15");
		blockSizeField.setPreferredSize(new Dimension(80, 20));
		blockSizeField.setColumns(4);
		GridBagConstraints gbc_blockSizeField = new GridBagConstraints();
		gbc_blockSizeField.anchor = GridBagConstraints.WEST;
		gbc_blockSizeField.insets = new Insets(0, 0, 5, 0);
		gbc_blockSizeField.gridx = 2;
		gbc_blockSizeField.gridy = 11;
		frmPropertreeMailerApplication.getContentPane().add(blockSizeField, gbc_blockSizeField);

		JLabel lblPausePerEmail = new JLabel("Pause Per Email");
		GridBagConstraints gbc_lblPausePerEmail = new GridBagConstraints();
		gbc_lblPausePerEmail.anchor = GridBagConstraints.WEST;
		gbc_lblPausePerEmail.insets = new Insets(0, 0, 5, 5);
		gbc_lblPausePerEmail.gridx = 1;
		gbc_lblPausePerEmail.gridy = 12;
		frmPropertreeMailerApplication.getContentPane().add(lblPausePerEmail, gbc_lblPausePerEmail);

		pausePerEmailField = new JTextField();
		pausePerEmailField.setText("1000");
		pausePerEmailField.setPreferredSize(new Dimension(80, 20));
		pausePerEmailField.setColumns(4);
		GridBagConstraints gbc_pausePerEmailField = new GridBagConstraints();
		gbc_pausePerEmailField.anchor = GridBagConstraints.WEST;
		gbc_pausePerEmailField.insets = new Insets(0, 0, 5, 0);
		gbc_pausePerEmailField.gridx = 2;
		gbc_pausePerEmailField.gridy = 12;
		frmPropertreeMailerApplication.getContentPane().add(pausePerEmailField, gbc_pausePerEmailField);

		JLabel lblPausePerBlock = new JLabel("Pause Per Block");
		GridBagConstraints gbc_lblPausePerBlock = new GridBagConstraints();
		gbc_lblPausePerBlock.anchor = GridBagConstraints.WEST;
		gbc_lblPausePerBlock.insets = new Insets(0, 0, 5, 5);
		gbc_lblPausePerBlock.gridx = 1;
		gbc_lblPausePerBlock.gridy = 13;
		frmPropertreeMailerApplication.getContentPane().add(lblPausePerBlock, gbc_lblPausePerBlock);

		pausePerBlockField = new JTextField();
		GridBagConstraints gbc_pausePerBlockField = new GridBagConstraints();
		gbc_pausePerBlockField.anchor = GridBagConstraints.WEST;
		gbc_pausePerBlockField.insets = new Insets(0, 0, 5, 0);
		gbc_pausePerBlockField.gridx = 2;
		gbc_pausePerBlockField.gridy = 13;
		frmPropertreeMailerApplication.getContentPane().add(pausePerBlockField, gbc_pausePerBlockField);
		pausePerBlockField.setText("5000");
		pausePerBlockField.setPreferredSize(new Dimension(80, 20));
		pausePerBlockField.setColumns(4);

		JLabel lblDelayedStart = new JLabel("Delayed Start");
		GridBagConstraints gbc_lblDelayedStart = new GridBagConstraints();
		gbc_lblDelayedStart.anchor = GridBagConstraints.WEST;
		gbc_lblDelayedStart.insets = new Insets(0, 0, 5, 5);
		gbc_lblDelayedStart.gridx = 1;
		gbc_lblDelayedStart.gridy = 14;
		frmPropertreeMailerApplication.getContentPane().add(lblDelayedStart, gbc_lblDelayedStart);

		delayedStartField = new JTextField();
		delayedStartField.setText("2000");
		delayedStartField.setPreferredSize(new Dimension(80, 20));
		delayedStartField.setColumns(4);
		GridBagConstraints gbc_delayedStartField = new GridBagConstraints();
		gbc_delayedStartField.anchor = GridBagConstraints.WEST;
		gbc_delayedStartField.insets = new Insets(0, 0, 5, 0);
		gbc_delayedStartField.gridx = 2;
		gbc_delayedStartField.gridy = 14;
		frmPropertreeMailerApplication.getContentPane().add(delayedStartField, gbc_delayedStartField);

		JLabel lblNewLabel_1 = new JLabel("               ");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 0);
		gbc_lblNewLabel_1.gridx = 2;
		gbc_lblNewLabel_1.gridy = 15;
		frmPropertreeMailerApplication.getContentPane().add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		JLabel lblOutput = new JLabel("Output");
		GridBagConstraints gbc_lblOutput = new GridBagConstraints();
		gbc_lblOutput.anchor = GridBagConstraints.WEST;
		gbc_lblOutput.insets = new Insets(0, 0, 5, 5);
		gbc_lblOutput.gridx = 1;
		gbc_lblOutput.gridy = 16;
		frmPropertreeMailerApplication.getContentPane().add(lblOutput, gbc_lblOutput);
		
		String sep = System.getProperty("line.separator");
		//String temp = "line one" + sep + "line one" + sep + "line one" + sep + "line one" + sep + "line one" + sep + "line one" + sep + "line one" + sep + "line one" + sep + "line one" + sep + "line one" + sep + "line one" + sep + "line one" + sep ;
		outputField = new JTextArea();
		outputField.setEditable(false);
		JScrollPane scrollPane = new JScrollPane(outputField);
		scrollPane.setPreferredSize(new Dimension(outputField.getX(), outputField.getY()));
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		outputField.setLineWrap(true);
		outputField.setWrapStyleWord(true);

		//outputField.setText(temp);
		GridBagConstraints gbc_outputField = new GridBagConstraints();
		gbc_outputField.insets = new Insets(0, 0, 5, 0);
		gbc_outputField.fill = GridBagConstraints.BOTH;
		gbc_outputField.gridx = 2;
		gbc_outputField.gridy = 16;
		frmPropertreeMailerApplication.getContentPane().add(scrollPane, gbc_outputField);

		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.anchor = GridBagConstraints.NORTH;
		gbc_panel.fill = GridBagConstraints.HORIZONTAL;
		gbc_panel.gridx = 2;
		gbc_panel.gridy = 17;
		frmPropertreeMailerApplication.getContentPane().add(panel, gbc_panel);
		panel.setLayout(new FlowLayout());

		final JButton startMailerButton = new JButton("Start");
		startMailerButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				startMailerButton.setEnabled(false);
				ExecutorService es = Executors.newSingleThreadExecutor();
				Future<Boolean>future = es.submit(new MailPublisher());
				try {
					future.get();
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				} catch (ExecutionException e1) {
					e1.printStackTrace();
				}
				startMailerButton.setEnabled(true);
			}
		});
		panel.add(startMailerButton);

		JSeparator separator = new JSeparator();
		panel.add(separator);

		JLabel lblNewLabel = new JLabel("               ");
		panel.add(lblNewLabel);

		JButton closeMailerButton = new JButton("Close");
		panel.add(closeMailerButton);

		JLabel label = new JLabel("               ");
		panel.add(label);

		JButton abortMailerButton = new JButton("Abort");
		panel.add(abortMailerButton);

	}

	private String setFilePath(JTextField textField) {
		JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		int returnVal = fc.showOpenDialog(fc);
		String filePath = null;
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			textField.setText(file.getAbsolutePath());
			filePath = file.getAbsolutePath();
		}
		return filePath;
		// System.out.println("ActionEvent :: " + e);
	}

}
